<?php
/**
 * Miffos framework
 * ----------------------------------------------
 *
 * Copyright (c) 2008, 2009 Martin Sadový (http://sodae.cz)
 *
 * This source file is subject to the "Miffos license" that is bundled
 * with this package in the file license.txt.
 *
 * @copyright  Copyright (c) 2008, 2009 Martin Sadový
 * @license    license.txt
 * @link       http://miffos.sodae.cz
 * @package    miffos
 * @version    $Id$
 */

class Debug # v2.0
{ // BEGIN class Debug

  private   static $template = null;
  private   static $types = array();
  public    static $out = array();
  private   static $dibi;

  /* FirePHP */
  const LOG = 'LOG';
  const INFO = 'INFO';
  const WARN = 'WARN';
  const ERROR = 'ERROR';
  const DUMP = 'DUMP';
  const TRACE = 'TRACE';
  const EXCEPTION = 'EXCEPTION';
  const TABLE = 'TABLE';
  const GROUP_START = 'GROUP_START';
  const GROUP_END = 'GROUP_END';
  
  
  /**
   * Zafixování definic
   */     
  private static function fix()
  {
    if (!E_RECOVERABLE_ERROR) {
      define('E_RECOVERABLE_ERROR', 4096);
    }
    if (!defined('E_DEPRECATED')) {
			define('E_DEPRECATED', 8192);
		}
  }
  
  /**
   * Startování debugeru
   * @param int     $run
   * @param string  $template
   */         
  public static function run($run = 6135,$template=NULL)
  { // BEGIN function run
      
      self::$template = is_null($template)?dirname(__FILE__)."/Application/Templates/debug.phtml":$template;
      
      self::fix();
      
      error_reporting($run);

      set_error_handler(array(__CLASS__, 'readerror'));
      
      set_exception_handler(array(__CLASS__, 'readexception'));
      
      
  } // END function run
  
  /**
   * Nastavení error reportingu
   * @param int $num
   */         
  public static function setErrorReport($num)
  { // BEGIN function setErrorReport
  	error_reporting($num);
  } // END function setErrorReport
  
  /**
   * Zastavení debugeru
   */ 
  public static function stop()
  { // BEGIN function stop
  
      error_reporting();
      
      restore_exception_handler();
      
      restore_error_handler();
      
  } // END function stop
  
  /**
   * Připraví informace z normálního erroru pro šablonu
   * @parms mixed all = work only php
   * @return call template
   */      
  public static function readerror($errno, $errstr, $errfile, $errline, $errtext)
  { // BEGIN function readerror
  

  
  	self::$types = array( // all error from php.net/set_error_handler
                   E_WARNING => 'Warning',
                   E_CORE_WARNING => 'Core warning',
                   E_COMPILE_WARNING => 'Compile warning',
                   E_USER_WARNING => 'Warning',
                   E_NOTICE => 'Notice',
                   E_USER_NOTICE => 'Notice',
                   E_STRICT => 'Strict standards',
                     # UP normal error | DOWN fatal error
                   E_ERROR => 'Fatal error',
                   E_CORE_ERROR => 'Fatal core error',
                   E_COMPILE_ERROR => 'Fatal compile error',
                   E_USER_ERROR => 'Fatal error',
                   E_PARSE => 'Parse error',
                   E_RECOVERABLE_ERROR => 'Catchable fatal error',
                   );
                   
    if(($errno & error_reporting()) == $errno){
        
       $trace =  self::gettrace();
       
       self::$out['type']  = self::$types[$errno];
       self::$out['text']  = $errstr;
       self::$out['file']  = $errfile;
       self::$out['line']  = $errline;
       self::$out['trace'] = $trace;
       self::$out['dibi']  = self::$dibi;
      
       
       self::calltemplate();
       
    }
    
  } // END function readerror
  
  
  /**
   * Připraví informace z Exception pro šablonu
   * @parms mixed all = work only php
   * @return call template
   */      
  public static function readexception(Exception $ex)
  { // BEGIN function readexception
  
  	   self::$out['type']  = "Exception";
       self::$out['text']  = $ex->getMessage();
       self::$out['file']  = $ex->getFile();
       self::$out['line']  = $ex->getLine();
       self::$out['trace'] = $ex->getTrace();
       self::$out['dibi']  = self::$dibi;

       self::calltemplate();
       
  } // END function readexception
  /**
   * Vypíše cestu
   * @return array
   */      
  public static function gettrace()
  { // BEGIN function gettrace
  
  	return debug_backtrace();
  	
  } // END function gettrace
  
  /**
   * Vyvolá šablonu
   * @return string
   */
  public static function calltemplate()
  { // BEGIN function calltemplate
    if( file_exists(self::$template) ){
      extract(self::$out);
      require_once self::$template;
    }else{
      echo "<strong>".self::$out['type'].":</strong>".self::$out['text']."<br>";
      echo "<strong>".self::$out['file'].":</strong>".self::$out['line']."<br><hr>";
    }
  } // END function calltemplate
  
  /**
   * Error log pro dibi
   */
  public static function addcolophon() // special function for dibi layer
  { // BEGIN function addcolophon
  	self::$dibi = dibi::getColophon();
  } // END function addcolophon

  /**
   * Dump for firefox firephp
   * @param string $k
   * @param mixed $v
   * @return
   */           
  public static function fireDump(string $k,$v){
  
      return self::fireEvent(array((string) $k => $v),2);
  
  }
  
  /**
   * call Debug::fireEvent()
   * @param mixed $value
   * @param constant $type
   * @param string|null $name
   * @return 
   */ 
  public static function fireLog($value,$type=self::LOG,$name=null){
     
     return self::fireEvent(array(array('Type' => $type, 'Label' => $name), $value),1);

  }

  /**
   * Post information firefox firephp
   * @param mixed $i
   */         
  private static function fireEvent($value,$i){
  
    static $line = 0;
    header('X-Wf-Protocol-miffos: http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
		header('X-Wf-miffos-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.2.0');

   if($i==2) {
    	header('X-Wf-miffos-Structure-2: http://meta.firephp.org/Wildfire/Structure/FirePHP/Dump/0.1');
   } else {
    	header('X-Wf-miffos-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
   }
   
   $value = json_encode($value);
   $line = $line+1;
   header("X-Wf-miffos-".$i."-1-n".$line.": |".$value."|");
  	
  }
  
}

?>